package com.xiaomi.baidumap.network;

import com.xiaomi.baidumap.model.AccessToken;
import com.xiaomi.baidumap.model.HotUserResult;
import com.xiaomi.baidumap.model.RepoContentResult;
import com.xiaomi.baidumap.model.RepoResult;
import com.xiaomi.baidumap.model.User;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Created by Administrator on 16-8-29.
 */
public class GithubClient implements GithubApi{

    private GithubApi githubApi;
    //设置为单例模式
    private static GithubClient githubClient;

    public static synchronized GithubClient getInstance(){
        if (githubClient == null){
            githubClient = new GithubClient();
        }
        return githubClient;
    }
    private GithubClient(){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //添加token拦截器
                .addInterceptor(new TokenInterceptor())
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .client(okHttpClient)
                //Retrofit 有一个强大的功能,Gson转换，将我们请求的数据进行自动json转换，并且转换成我们需要的数据类型
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        githubApi = retrofit.create(GithubApi.class);

    }

    @Override
    public Call<AccessToken> getOAuthToken(@Field("client_id") String clientId, @Field("client_secret") String clientSecret, @Field("code") String code) {
        return githubApi.getOAuthToken(clientId,clientSecret,code);
    }


    @Override public Call<User> getUser() {
        return githubApi.getUser();
    }

    @Override
    public Call<RepoResult> searchRepos(@Query("q") String qury, @Query("page") int pageId) {
        return githubApi.searchRepos(qury,pageId);
    }

    @Override
    public Call<RepoContentResult> getReadme(@Path("owner") String owner, @Path("repo") String repo) {
        return githubApi.getReadme(owner,repo);
    }

    @Override
    public Call<ResponseBody> markDown(@Body RequestBody body) {
        return githubApi.markDown(body);
    }

    @Override
    public Call<HotUserResult> searchUsers(@Query("q") String query, @Query("page") int pageId) {
        return githubApi.searchUsers(query,pageId);
    }
}
